<?php
class MyAccount_Controller extends UserController {

    public function __construct() {
        parent::__construct();
        $this->activeMenuItem = UserMenu::$MY_ACCOUNT;
    }

    public function display() {
        $person = $this->dao->fetchPersonById($this->getAuthOrSubstituteUser('id'));
        $this->viewData['account'] = $person;
        $this->loadFormData();
        $this->renderSubview('user/myAccount/myAccountDisplay');
    }

    public function edit() {
        $this->loadFormData();

        $this->viewData['person'] = $this->dao->fetchPersonById($this->getAuthOrSubstituteUser('id'));
        $this->renderSubview('user/myAccount/myAccountEdit');
    }

    protected function loadFormData() {
        $this->viewData['countryOptions'] = $this->dao->optionsCountry();
        $this->viewData['idcardTypeOptions'] = form::idcardOptions();
        $this->viewData['organizationOptions'] = $this->dao->optionsOrganizationsByCountry();
    }

    public function save() {
        $person = $this->postToDbValues();
        
        if ($person['id'] != $this->getAuthOrSubstituteUser('id')) {
        	logger::securityAlert('Attempt to modify a user with ID ' . $person['id']);
        	$this->msg->error('error.invalidId');
        	$this->redirect('/user/myAccount/display');
        	return;
        }

        $this->viewData['person'] = $person;

        // Basic validation
        $validator = new EntityValidator($person, 'person', $this->msg);
        $validator->required('first_name');
        $validator->required('last_name');
        $validator->required('gender');
        $validator->date('birth_date');
        $validator->required('country_code');
        $validator->required('email');
        $validator->idOrNull('organization_id');
        $validator->applicationLanguage('preferred_language');

        if ($this->msg->hasErrors()) {
            $this->loadFormData();
            $this->renderSubview('user/myAccount/myAccountEdit');
            return;
        }

        // Database validation
        if ($person['organization_id']) {
	        $organization = $this->dao->getById('organization', $person['organization_id']);
	        if (! $organization) {
	        	$this->msg->error('error-recordNotFound');
	        	$this->loadFormData();
	        	$this->renderSubview('user/myAccount/myAccountEdit');
	        	return;
	        }
        }
        
        $uniqueFields = array ('first_name', 'last_name', 'birth_date');
        if ($this->dao->alreadyExists('person', $uniqueFields, $person)) {
            $this->loadFormData();
            $this->msg->error('person.error-alreadyExists-names');
            $this->renderSubview('user/myAccount/myAccountEdit');
            return;
        }

        // Insert or update the seminar
        $this->dao->update('person', $person);
        $this->msg->info('userSpace-account-editSuccess');
        
        $person = $this->dao->fetchPersonById($this->getAuthOrSubstituteUser('id'));
        $this->getSessionData()->setAuthUser($person);

        $this->redirect('/user/myAccount/display');
    }

    public function changePassword() {
        $this->viewData['user'] = array('userLogin' => $this->getAuthOrSubstituteUser('login'));
        $this->renderSubview('user/myAccount/changePassword');
    }

    public function saveNewPassword() {
        $formData = $this->postToDbValues();
        $newPassword = $formData['userPassword'];
        if (strlen($newPassword) < auth::$PASSWORD_MIN_LENGTH) {
            $this->msg->info('person.error-passwordTooShort', auth::$PASSWORD_MIN_LENGTH);
            return $this->changePassword();
        }
        
        $credentials = array(
        		'person_id' => $this->getAuthOrSubstituteUser('id'), 
        		'password' => auth::passwordHash($this->getAuthOrSubstituteUser('login'), $newPassword));
        $this->dao->update('account', $credentials, 'person_id');
        $this->msg->info('userSpace.changePassword-success');
        $this->redirect('/user/myAccount/display');
    }
}
